#!/usr/bin/env python

import sys
import pytest
from datetime import datetime
import argparse


def main():
    parser = argparse.ArgumentParser(description='The test toolkit for LiangJin WeiMu projects')
    parser.add_argument("-v", "--verbose", dest='verbose', action='store_true',
                        help="increase output verbosity to track issue")
    parser.add_argument("cases", metavar='C', nargs='*', default=['case'], help='case file or directory, default: case')
    parser.add_argument("-nw", "--no-warm", dest='warm', action='store_false', help="no warm cases")
    parser.add_argument("-wc", "--warm-cases", metavar='WC',  nargs='*', default=['warmup'],
                        help="warm case file or directory, default: warmup")

    parser.add_argument("-c", "--cleanup", dest='cleanup', action='store_true', help="run cleanup cases")
    parser.add_argument("-cc", "--cleanup-cases", metavar='CC', nargs='*', default=['cleanup'],
                        help="clean case file or directory, default: cleanup")

    parser.add_argument("-r", "--report", dest='report', const=True, nargs='?', help="output html report ")
    parser.add_argument("-s", "--skip", dest='skip', action='store_true', help="skip run tests, only for debug")
    args = parser.parse_args()
    if args.verbose:
        print("Parsed args: {}".format(args))
    pytest_args = ['-x']
    if args.warm:
        pytest_args += args.warm_cases
    pytest_args += args.cases
    if args.cleanup:
        pytest_args += args.cleanup_cases
    if args.report:
        if type(args.report) is str:
            pytest_args.append('--html={}'.format(args.report))
        else:
            pytest_args.append('--html=report/report_{}.html'.format(datetime.now().strftime('%Y-%m-%d_%H_%M_%S')))
    if args.verbose:
        print("Run pytest with {}".format(pytest_args))
    if not args.skip:
        ret = pytest.main(pytest_args)
        return ret
    return 0


if __name__ == '__main__':
    sys.exit(main())
